<template>
  <div>
    <h1 style="color: green">4月份考试</h1>
    <el-container style="height: 500px; border: 1px solid #eee">
      <el-container>
        <!-- 头部 -->
        <el-header style="text-align: right; font-size: 12px">
          <div style="width: 100%; height: 60px">
            <div style="width: 1000px; height: 60px;text-align: left">
              <i class="el-icon-s-unfold" style="width: 50px; height: 30px"></i>
              <span style="width: 50px;height: 30px;">
              </span>
            </div>

          </div>
        </el-header>
        <!-- 子路由(身体) -->
        <el-main>
          <div style="text-align: left" class="div1">
            <span>4月份考试</span>
            <br />
            <el-input placeholder="名称" v-model="input1" style="width:200px;margin-right:20px"></el-input>
            <!-- <el-select v-model="input2" placeholder="商品类型">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
            </el-select> -->
            

            <!--查询-->
            <el-button type="info" icon="el-icon-search" @click="select()">查询</el-button>
            <!--添加按钮-->
            <el-button type="success" icon="el-icon-edit" @click="dialogVisible = true">新增</el-button>
            <!--添加模态框-->
            <el-dialog title="新增水果信息" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
              <span>新增水果信息</span>
              <br />
              <div class="addinput">
                <el-input placeholder="商品名称" v-model="addgoodsName" style="width: 300px"></el-input>
                <el-input placeholder="商品数量" v-model="addgoodsamount" style="width: 300px"></el-input>
                <el-select v-model="addTypeId" placeholder="商品类型">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </div>
              <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="save(), (dialogVisible = false)">确 定</el-button>
              </span>
            </el-dialog>

            <!--修改模态框-->
            <el-dialog title="修改水果信息" :visible.sync="updialogVisible" width="30%" :before-close="handleClose">
              <span>修改水果信息</span>
              <br />
              <div class="addinput">
                <el-input placeholder="商品名称" v-model="upgoodsName" style="width: 300px"></el-input>
                <el-input placeholder="商品数量" v-model="upgoodsamount" style="width: 300px"></el-input>
                <el-select v-model="upTypeId" placeholder="商品类型">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
                <el-input placeholder="pkId" v-model="uppkId" style="width: 300px" v-show="false"></el-input>
              </div>
              <span slot="footer" class="dialog-footer">
                <el-button @click="updialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="update(), (updialogVisible = false)">确 定</el-button>
              </span>
            </el-dialog>
          </div>


          <el-table :data="tableData">
            <el-table-column prop="goodsName" label="商品名称" width="120">
            </el-table-column>
            <el-table-column prop="goodsAmount" label="商品数量" width="140">
            </el-table-column>
            <template slot-scope=""><!-- 类型转换 -->
                <el-table-column label="商品类型" width="180">
                  <template slot-scope="scope">
                    <span v-html="typeName1(scope.row.typeId)"></span>
                  </template>
                </el-table-column>
            </template>
            
            <template slot-scope=""><!-- 时间转换 -->
              <el-table-column label="创建时间" width="400">
                <template slot-scope="scope">
                  <span v-html="getYMDHMS(scope.row.createTime)"></span>
                </template>

              </el-table-column>
              <el-table-column label="修改时间" width="400">
                <template slot-scope="scope">
                  <span v-html="getYMDHMS(scope.row.updateTime)"></span>
                </template>

              </el-table-column>
            </template>

            <el-table-column label="操作">
              <template slot-scope="scope">
                <!--修改按钮-->
                <el-button type="text" @click="find(scope.row.pkId),(updialogVisible = true)">修改</el-button>
                <!--删除按钮-->
                <el-button size="mini" type="danger"  @click="del(scope.row.pkId)">删除</el-button>

              </template>
            </el-table-column>
          </el-table>
          <!-- 分页 -->
          <el-row>
            <el-col style="text-align: right">
              <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageNum"
                :page-sizes="[5, 10, 15, 20]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper"
                :total="total">

              </el-pagination>
            </el-col>
          </el-row>

        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "kaoshi3",
   //页面自动加载，
   created: function () {
    this.select();
  },
  data() {
    return {
      //分页
      pageNum: 1, //默认当前页数
      pageSize: 5, //默认每页显示条数
      total: 0, //总数据条数

      dialogVisible : false,
      addgoodsName : "",
      addgoodsamount : "",
      addTypeId : "",
      options: [
        {
          value: "1",
          label: "水果",
        },
        {
          value: "2",
          label: "蔬菜",
        },
        {
          value: "3",
          label: "副食品",
        },
        {
          value: "4",
          label: "日用品",
        },
      ],
      updialogVisible : false,
      upgoodsName : "",
      upgoodsamount : "",
      upTypeId : "",
      uppkId : "",
      tableData: [],
    };
  },
  props: {},
  methods: {
    select : function(){
      var that = this ;
      this.$axios.post("Ssbcontroller6/quest",{
        //分页
        pageNum: that.pageNum,
        pageSize: that.pageSize,
      })
      .then(function(response){
        that.tableData = response.data.data;
        that.total = response.data.pageInfo.total;
      }
      )
      
    },
    save : function(){
      var that = this ;
      this.$axios.post("Ssbcontroller6/save",{
        goodsName : that.addgoodsName,
        goodsAmount : that.addgoodsamount,
        typeId : that.addTypeId,
      })
      .then(function(response){
        that.select(),
            that.$message({
              //弹出框
              message: "新增成功!",
              type: "success",
          });
      })
    },
    del : function(pkId){
      var that = this; 
      this.$axios.post("Ssbcontroller6/del",{
        pkId : pkId
      })
      .then(function(response){
        that.select(),
            that.$message({
              //弹出框
              message: "删除成功!",
              type: "success",
          });
      })
    },
    find : function(pkId){
      var that = this ;
      this.$axios.post("Ssbcontroller6/find",{
        pkId : pkId
      })
      .then(function(response){
        if (response.data.code == "3333") {
            that.upgoodsName = response.data.data.goodsName;
            that.upgoodsamount = response.data.data.goodsAmount;
            that.upTypeId = response.data.data.typeId;
            that.uppkId = response.data.data.pkId;
          } else {
            //alert("修改失败");
          }
      })
    },
    update : function(){
      var that =this;
      this.$axios.post("Ssbcontroller6/update",{
        goodsName : that.upgoodsName,
        goodsAmount : that.upgoodsamount,
        typeId : that.upTypeId,
        pkId : that.uppkId,
      })
      .then(function(response){
        that.select()
      })
    },
    // 获取当前页的数据信息
    //pageSize 默认每页显示条数改变时会触发
    handleSizeChange(val) {
      this.pageSize = val;
      this.select(this.pageNum, this.pageSize); //console.log(`每页 ${val} 条`);
    }, //currentPage 默认当前页数改变时会触发
    handleCurrentChange(val) {
      this.pageNum = val;
      this.select(this.pageNum, this.pageSize); //console.log(`当前页: ${val}`);
    },
     //时间格式转换
     getYMDHMS: function (timestamp) {
      let time = new Date(timestamp);
      let year = time.getFullYear();
      let month = time.getMonth() + 1;
      let date = time.getDate();
      let hours = time.getHours();
      let minute = time.getMinutes();
      let second = time.getSeconds();

      if (month < 10) {
        month = "0" + month;
      }
      if (date < 10) {
        date = "0" + date;
      }
      if (hours < 10) {
        hours = "0" + hours;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      if (second < 10) {
        second = "0" + second;
      }return (
        year +
        "-" +
        month +
        "-" +
        date +
        " " +
        hours +
        ":" +
        minute +
        ":" +
        second
      );
    },
    //类型转换
    typeName1(typeId) {
      if (typeId == "1") {
        return "蔬菜";
      } else if (typeId == "2") {
        return "水果";
      } else if (typeId == "3") {
        return "副食品";
      } else if (typeId == "4") {
        return "日用品";
      }

    },

  },
};
</script>

<style scoped></style>